package com.unionftech.crm_android.ui

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.unionftech.common.R.dimen
import com.unionftech.common.R.string
import com.unionftech.common.base.mvp.impl.BaseMvpActivity
import com.unionftech.common.common.bean.AppVersionInfo
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.MarketUtils
import com.unionftech.common.utils.Utils
import com.unionftech.common.utils.parcelable
import com.unionftech.crm_android.R

class UFUpdateActivity : BaseMvpActivity() {
    private val mBtnNotUpdate by lazy { findViewById<TextView>(R.id.btn_not_update) }
    private val mBtnUpdate by lazy { findViewById<Button>(R.id.btn_update) }
    private val mTvUpdateMsg by lazy { findViewById<TextView>(R.id.tv_update_msg) }
    private val mTvUpdateTitle by lazy { findViewById<TextView>(R.id.tv_update_title) }
    private val mTvVersionName by lazy { findViewById<TextView>(R.id.tv_version_name) }
    private var mAppVersionInfo: AppVersionInfo? = null

    companion object {
        private const val PARAMS_VERSION_INFO = "params_version_info"

        @JvmStatic
        fun startThis(context: Context, appVersionInfo: AppVersionInfo) {
            Intent(context, UFUpdateActivity::class.java).apply {
                if (context !is Activity) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                putExtra(PARAMS_VERSION_INFO, appVersionInfo)
                context.startActivity(this)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_ufupdate

    override fun obtainIntentData(intent: Intent) {
        super.obtainIntentData(intent)
        mAppVersionInfo = intent.parcelable(PARAMS_VERSION_INFO)
    }

    override fun initLanguageText() {
        super.initLanguageText()
        mTvUpdateTitle.text =
            DynamicResourceManager.getString(string.have_new_version)
        mBtnUpdate.text = DynamicResourceManager.getString(string.update_now)
        mBtnNotUpdate.text = DynamicResourceManager.getString(string.not_update)
    }

    override fun setListener() {
        super.setListener()
        mBtnNotUpdate.setOnClickListener(this)
        mBtnUpdate.setOnClickListener(this)
    }

    /**
     * 屏蔽物理返回键
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    @SuppressLint("SetTextI18n")
    override fun setView() {
        super.setView()
        hideTitleBar()
        mTvUpdateTitle.post {
            mTvUpdateTitle.measure(0, 0)
            val measuredWidth = mTvUpdateTitle.measuredWidth
            LogUtils.d("UFUpdateActivity", "measuredWidth -->${measuredWidth}")
            val layoutParams =
                LinearLayout.LayoutParams(measuredWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
            layoutParams.topMargin =
                mContext.resources.getDimension(dimen.dp_16).toInt()
            mTvUpdateMsg.layoutParams =
                layoutParams
        }
        mAppVersionInfo?.also { info ->
            mTvVersionName.text =
                DynamicResourceManager.getString(string.version) + " " + info.versionCode
            if (info.isForceUpdate == 0) {
                //强制更新
                mBtnNotUpdate.visibility = View.GONE
            } else {
                mBtnNotUpdate.visibility = View.VISIBLE
            }
            //更新内容
            mTvUpdateMsg.text = info.forceUpdateRemark
        }
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.btn_update -> {
                MarketUtils.startMarket(mContext)
            }

            R.id.btn_not_update -> {
                finish()
            }
        }
    }

}